﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP
{
    public partial class AdvertisementAdd : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "/uploads/ads/";

            if (FileUpload1.HasFile)
            {
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(uploadFolder + now + extension);
                UploadStatusLabel.Text = "File uploaded successfully as: ";
                imgName.Text = now + extension;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlCommand cmdInsert, cmdSelect;
            conn.Open();

            cmdInsert = new SqlCommand("INSERT INTO Advertisements VALUES(@a_link, @a_status, @a_picture, @a_duration, @date, @price)", conn);
            cmdInsert.Parameters.AddWithValue("@a_link", "http://" + txtLink.Text);
            cmdInsert.Parameters.AddWithValue("@a_status", "pending");
            cmdInsert.Parameters.AddWithValue("@a_picture", imgName.Text);
            cmdInsert.Parameters.AddWithValue("@a_duration", txtDuration.Text);

            DateTime now = DateTime.Now;
            cmdInsert.Parameters.AddWithValue("@date", now);

            cmdSelect = new SqlCommand("SELECT ch_price FROM Charges WHERE ch_type='Ads'", conn);
            double price = Convert.ToDouble(cmdSelect.ExecuteScalar());
            price = price * Convert.ToDouble(txtDuration.Text);
            cmdInsert.Parameters.AddWithValue("@price", price);

            int success = cmdInsert.ExecuteNonQuery();
            if (success == 1)
                {
                    MessageBox("Upload Successfully!");
                    //Redirect("ProductsList.aspx");
                }

            conn.Close();
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}